const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const globalVars = require('./src/utils/var.js')
let glob = require('glob')

function resolve (dir) {
    return path.join(__dirname, dir)
}

// 配置pages多页面获取当前文件夹下的html和js
function getEntry (globPath) {
    let entries = {}; let tmp; let htmls = {}
    // 读取src/pages/**/底下所有的html文件
    glob.sync(globPath + 'html').forEach(function (entry) {
        tmp = entry.split('/').splice(-3)
        htmls[tmp[1]] = entry
    })
    // 读取src/pages/**/底下所有的js文件
    glob.sync(globPath + 'js').forEach(function (entry) {
        tmp = entry.split('/').splice(-3)
        entries[tmp[1]] = {
            entry,
            template: htmls[tmp[1]] ? htmls[tmp[1]] : 'index.html', //  当前目录没有有html则以共用的public/index.html作为模板
            filename: tmp[1] + '.html' //  以文件夹名称.html作为访问地址
        }
    })
    return entries
}

let objectProject = {
    billPrint: {
        entry: 'src/plugins/billPrint/main.js',
        template: 'src/plugins/billPrint/billPrint.html',
        filename: 'billPrint.html',
        title: 'BillPrint Page',
        chunks: ['chunk-vendors', 'chunk-common', 'billPrint']
    },
    commision: {
        entry: 'src/plugins/commision/main.js',
        template: 'src/plugins/commision/commision.html',
        filename: 'commision.html',
        title: 'Commision Page',
        chunks: ['chunk-vendors', 'chunk-common', 'commision']
    },
    moneyDetail: {
        entry: 'src/plugins/moneyDetail/main.js',
        template: 'src/plugins/moneyDetail/moneyDetail.html',
        filename: 'moneyDetail.html',
        title: 'MoneyDetail Page',
        chunks: ['chunk-vendors', 'chunk-common', 'moneyDetail']
    },
    saleProfit: {
        entry: 'src/plugins/saleProfit/main.js',
        template: 'src/plugins/saleProfit/saleProfit.html',
        filename: 'saleProfit.html',
        title: 'saleProfit page',
        chunks: ['chunk-vendors', 'chunk-common', 'saleProfit']
    },
    saleRatioStatistics: {
        entry: 'src/plugins/saleRatioStatistics/main.js',
        template: 'src/plugins/saleRatioStatistics/saleRatioStatistics.html',
        filename: 'saleRatioStatistics.html',
        title: 'saleRatioStatistics page',
        chunks: ['chunk-vendors', 'chunk-common', 'saleRatioStatistics']
    },
    profitReport: {
        entry: 'src/plugins/profitReport/main.js',
        template: 'src/plugins/profitReport/profitReport.html',
        filename: 'profitReport.html',
        title: 'profitReport page',
        chunks: ['chunk-vendors', 'chunk-common', 'profitReport']
    },
    stockCheckEnter: {
        entry: 'src/plugins/stockCheck/stockCheckEnter/main.js',
        template: 'src/plugins/stockCheck/stockCheckEnter/stockCheckEnter.html',
        filename: 'stockCheckEnter.html'
    },
    stockCheckReplay: {
        entry: 'src/plugins/stockCheck/stockCheckReplay/main.js',
        template: 'src/plugins/stockCheck/stockCheckReplay/stockCheckReplay.html',
        filename: 'stockCheckReplay.html'
    },
    /** 总账管理 **/
    voucher: {
        entry: 'src/plugins/generalLedger/voucher/main.js',
        template: 'src/plugins/generalLedger/voucher/voucher.html',
        filename: 'voucher.html'
    },
    checkOut: {
        entry: 'src/plugins/generalLedger/checkOut/main.js',
        template: 'src/plugins/generalLedger/checkOut/checkOut.html',
        filename: 'checkOut.html'
    },
    detailed: {
        entry: 'src/plugins/generalLedger/accountBook/detailed/main.js',
        template: 'src/plugins/generalLedger/accountBook/detailed/detailed.html',
        filename: 'detailed.html'
    },
    allAccount: {
        entry: 'src/plugins/generalLedger/accountBook/allAccount/main.js',
        template: 'src/plugins/generalLedger/accountBook/allAccount/allAccount.html',
        filename: 'allAccount.html'
    },
    subjectBalance: {
        entry: 'src/plugins/generalLedger/accountBook/subjectBalance/main.js',
        template: 'src/plugins/generalLedger/accountBook/subjectBalance/subjectBalance.html',
        filename: 'subjectBalance.html'
    },
    subjectSummary: {
        entry: 'src/plugins/generalLedger/accountBook/subjectSummary/main.js',
        template: 'src/plugins/generalLedger/accountBook/subjectSummary/subjectSummary.html',
        filename: 'subjectSummary.html'
    },
    auxiliaryAccountingDetail: {
        entry: 'src/plugins/generalLedger/accountBook/auxiliaryAccountingDetail/main.js',
        template: 'src/plugins/generalLedger/accountBook/auxiliaryAccountingDetail/auxiliaryAccountingDetail.html',
        filename: 'auxiliaryAccountingDetail.html'
    },
    auxiliaryAccountingBalance: {
        entry: 'src/plugins/generalLedger/accountBook/auxiliaryAccountingBalance/main.js',
        template: 'src/plugins/generalLedger/accountBook/auxiliaryAccountingBalance/auxiliaryAccountingBalance.html',
        filename: 'auxiliaryAccountingBalance.html'
    },
    report: {
        entry: 'src/plugins/generalLedger/report/main.js',
        template: 'src/plugins/generalLedger/report/report.html',
        filename: 'report.html'
    },
    accountSets: {
        entry: 'src/plugins/generalLedger/setting/accountSets/main.js',
        template: 'src/plugins/generalLedger/setting/accountSets/accountSets.html',
        filename: 'accountSets.html'
    },
    currency: {
        entry: 'src/plugins/generalLedger/setting/currency/main.js',
        template: 'src/plugins/generalLedger/setting/currency/currency.html',
        filename: 'currency.html'
    },
    voucherWord: {
        entry: 'src/plugins/generalLedger/setting/voucherWord/main.js',
        template: 'src/plugins/generalLedger/setting/voucherWord/voucherWord.html',
        filename: 'voucherWord.html'
    },
    subject: {
        entry: 'src/plugins/generalLedger/setting/subject/main.js',
        template: 'src/plugins/generalLedger/setting/subject/subject.html',
        filename: 'subject.html'
    },
    initial: {
        entry: 'src/plugins/generalLedger/setting/initial/main.js',
        template: 'src/plugins/generalLedger/setting/initial/initial.html',
        filename: 'initial.html'
    },
    assistAccount: {
        entry: 'src/plugins/generalLedger/setting/assistAccount/main.js',
        template: 'src/plugins/generalLedger/setting/assistAccount/assistAccount.html',
        filename: 'assistAccount.html'
    },
    /** 首页 **/
    index: {
        entry: 'src/main.js',
        template: 'public/index.html',
        filename: 'index.html'
    }
}
let page = {}
let projectName = process.argv[3] // 获取执行哪个文件
if (projectName === 'produce') {
    page = getEntry('./src/plugins/produce/**/*.')
} else if (projectName === 'stockCheck') {
    page = getEntry('./src/plugins/stockCheck/**/*.')
} else if (projectName === 'generalLedger') {
    page = getEntry('./src/plugins/generalLedger/**/*.')
} else if (projectName === 'workflow') {
    page = getEntry('./src/plugins/workflow/**/*.')
} else {
    if (process.env.NODE_ENV === 'development') {
        page = objectProject
    } else {
        page[projectName] = objectProject[projectName]
    }
}

let publicPathStr, dirStr
if (projectName === undefined) {
    publicPathStr = '/'
    dirStr = 'dist'
} else {
    publicPathStr = './'
    dirStr = 'dist/' + projectName
}

// vue.config.js
module.exports = {
    outputDir: dirStr, // 标识是打包哪个文件
    filenameHashing: true,
    pages: page,
    // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
    productionSourceMap: false,
    // 静态文件的路径前缀
    publicPath: publicPathStr,
    configureWebpack: config => {
        // 生产环境取消 console.log
        if (process.env.NODE_ENV === 'production') {
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
        }
    },
    chainWebpack: (config) => {
        config.resolve.alias
            .set('@$', resolve('src'))
            .set('@api', resolve('src/api'))
            .set('@assets', resolve('src/assets'))
            .set('@comp', resolve('src/components'))
            .set('@views', resolve('src/views'))
        // 生产环境，开启js\css压缩
        if (process.env.NODE_ENV === 'production') {
            config.plugin('compressionPlugin').use(new CompressionPlugin({
                test: /\.(js|css|less)$/, // 匹配文件名
                threshold: 10240, // 对超过10k的数据压缩
                deleteOriginalAssets: false // 删除源文件
            }))
        }
    },
    css: {
        loaderOptions: {
            less: {
                globalVars,
                javascriptEnabled: true
            }
        }
    },
    devServer: {
        port: 3000,
        proxy: {
            '/jshERP-boot': {
                target: 'http://erp.ehymall.com', // 请求本地 需要jshERP-boot后台项目
                ws: false,
                changeOrigin: true
            }
        }
    },
    lintOnSave: undefined
}
